#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QFileDialog>
#include <QSettings>
#include <QStandardPaths>
#include <memory>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->lable_image->clear();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::open1()
{
    QString fileName = QFileDialog::getOpenFileName(this, "请选择图片", "C:/", "图片(*.png *.jpg);;");

    if(fileName.isEmpty())
    {
        return ;
    }

    ui->imageOpen->setText(fileName);
    ui->lable_image->setPixmap((QPixmap(fileName)));
}

void Widget::open2()
{
    QString config_path = qApp->applicationDirPath() + "/config/Setting.ini";
    qDebug() << config_path;

    // 智能指针
    std::unique_ptr<QSettings> pIniSet(new QSettings(config_path, QSettings::IniFormat));
    QString lastPath = pIniSet->value("/LastPath/path").toString();

    // 设置默认路径
    if(lastPath.isEmpty())
    {
        lastPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    }

    QString fileName = QFileDialog::getOpenFileName(this, "请选择图片", lastPath, "图片(*.png *.jpg);;");

    if(fileName.isEmpty())
    {
        return ;
    }

    ui->imageOpen->setText(fileName);
    ui->lable_image->setPixmap((QPixmap(fileName)));

    int end = fileName.lastIndexOf("/");
    QString _path = fileName.left(end);
    qDebug() << _path;
    pIniSet->setValue("/LastPath/path", _path);
}

void Widget::open3()
{
    QString config_path = qApp->applicationDirPath() + "/config/Setting.ini";
    qDebug() << config_path;

    QSettings *pIniSet = new QSettings(config_path, QSettings::IniFormat);
    QString lastPath = pIniSet->value("/LastPath/path").toString();

    // 设置默认路径
    if(lastPath.isEmpty())
    {
        lastPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    }

    QString fileName = QFileDialog::getOpenFileName(this, "请选择图片", lastPath, "图片(*.png *.jpg);;");

    if(fileName.isEmpty())
    {
        return ;
    }

    ui->imageOpen->setText(fileName);
    ui->lable_image->setPixmap((QPixmap(fileName)));

    int end = fileName.lastIndexOf("/");
    QString _path = fileName.left(end);
    qDebug() << _path;
    pIniSet->setValue("/LastPath/path", _path);

    delete pIniSet;
    pIniSet = nullptr;
}

void Widget::open4()
{
    QString config_path = qApp->applicationDirPath() + "/config/Setting.ini";
    qDebug() << config_path;

    QSettings *pIniSet = new QSettings(config_path, QSettings::IniFormat);
    QString lastPath = pIniSet->value("/LastPath/path").toString();

    // 设置默认路径
    if(lastPath.isEmpty())
    {
        lastPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    }

    QString fileName = QFileDialog::getOpenFileName(this, "请选择图片", lastPath, "图片(*.png *.jpg);;");

    if(fileName.isEmpty())
    {
        return ;
    }

    ui->imageOpen->setText(fileName);

    QPixmap *pix = new QPixmap(fileName);
    pix->scaled(ui->lable_image->size(), Qt::KeepAspectRatio);
    ui->lable_image->setScaledContents(true);

    ui->lable_image->setPixmap(*pix);

    delete pix;
    pix = nullptr;

    int end = fileName.lastIndexOf("/");
    QString _path = fileName.left(end);
    qDebug() << _path;
    pIniSet->setValue("/LastPath/path", _path);

    delete pIniSet;
    pIniSet = nullptr;
}

void Widget::on_btnOpen_clicked()
{
    // qDebug() << "clicked hello";

    // open1();
    // open2();
    // open3();

    open4();
}

